package com.xiangyi.teachingsystem.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.api.R;
import com.xiangyi.teachingsystem.entity.po.CourseRes;
import com.xiangyi.teachingsystem.entity.vo.Result;
import com.xiangyi.teachingsystem.service.CourseResService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
public class CourseResController {
    @Autowired
    CourseResService courseResService;

    @PostMapping("/addCourseRes")
    public Result addCourseRes(@Validated @RequestBody CourseRes courseRes) {
        CourseRes resInfo = courseResService.getOne(new QueryWrapper<CourseRes>()
                .eq("res_name", courseRes.getResName())
                .eq("course_id",courseRes.getCourseId()));
        if (resInfo != null)
            return Result.failure("资源名称已存在");
        try {
            courseResService.save(courseRes);
            return Result.success("资源上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return Result.failure("服务器错误").setData(e.getMessage());
        }
    }

    @GetMapping("/courseResList")
    public Result courseResList(@RequestParam Integer courseId) {
        return Result.success("课程资源列表查询成功").setData(courseResService.list(courseId));
    }

    @GetMapping("/delCourseRes")
    public Result delCourseRes(@RequestParam Integer id){
        try {
            courseResService.removeById(id);
            return Result.success("资源删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return Result.failure("服务器错误");
        }
    }
}
